package utilities;


/**
 * 
 * @author Mathieu Rousseau
 *
 * @email mathieu.rousseau3@mail.mcgill.ca
 *
**/


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;


/** ------------------------------------------------------------------------------------------------------------------
 * start of LOOCVextractor class
 * ------------------------------------------------------------------------------------------------------------------  **/
public class LOOCVextractor {

	
	/*  global variables  */
	private static String inputPath = "LOOCVextractor/Input/";
	/*  end of global variables  */
	
	
	/*  start of main method  */
	public static void main(String[] args){
		
		
		HashMap<String, ArrayList<Integer>> existingRuns = new HashMap<String, ArrayList<Integer>>();
		
		HashMap<String, ArrayList<String>> folderNameMapping = new HashMap<String, ArrayList<String>>();
		
		
		File inputFolder = new File(inputPath);
		String[] folderList = inputFolder.list();
		
//		int folderCounter = 0;
//		for(String folderName: folderList){
//			System.out.println(folderName);
//			folderCounter++;
//		}
//		System.out.println("folderList.length=" + folderList.length + "  folderCounter=" + folderCounter);
		
		
		for(String folderName: folderList){
			if(folderName.equals(".DS_Store")){
//				System.out.println("Skipping the folder: " + folderName);
				continue;
			}
			
			File currentFolder = new File(inputPath + folderName);
			String[] fileList = currentFolder.list();
			
//			System.out.println(folderName);
			
//			System.out.println(fileList.length);
			if(fileList.length != 5){
				if((fileList.length == 6) && (fileList[0].equals(".DS_Store"))){
					//  do nothing
				}
				else{
					System.out.println(folderName + "\t" + fileList.length);
				}
			}
			

			int fileCounter = 0;
			for(String fileName: fileList){
				if(fileName.contains("parameters")){
					//				System.out.println(fileName);

					String splitFilename[] = fileName.split("_");
					int indexInt = Integer.valueOf(splitFilename[1]);
					String indexString = splitFilename[1];
					//				System.out.println("index=" + indexInt);


					String exponentString = "0";
					String line = null;
					try{
						BufferedReader br = new BufferedReader(new FileReader(currentFolder + "/" + fileName));			
						while((line = br.readLine()) != null){
							String[] values = line.split("\t");

							if(values[0].equals("exponent=")){
								double exponentDouble = Double.parseDouble(values[1]);
								exponentString = values[1];
//								System.out.println("exponent=" + exponentDouble);
							}
						}
						br.close();
					}catch (IOException e) {
						e.printStackTrace();
					}
					
					if(!exponentString.equals("0")){
						//  store all of the indices that were run for each of the exponents
						if(existingRuns.containsKey(exponentString)){
							ArrayList<Integer> existingListOfIndices = existingRuns.get(exponentString);
							existingListOfIndices.add(indexInt);
							existingListOfIndices.trimToSize();
							existingRuns.put(exponentString, existingListOfIndices);
						}
						else{
							ArrayList<Integer> newListOfIndices = new ArrayList<Integer>();
							newListOfIndices.add(indexInt);
							newListOfIndices.trimToSize();
							existingRuns.put(exponentString, newListOfIndices);
						}
						
						//  store all of the folder names for each of the exponents
						if(folderNameMapping.containsKey(exponentString)){
							ArrayList<String> existingListOfFolderNames = folderNameMapping.get(exponentString);
							existingListOfFolderNames.add(folderName);
							existingListOfFolderNames.trimToSize();
							folderNameMapping.put(exponentString, existingListOfFolderNames);
						}
						else{
							ArrayList<String> newListOfFolderNames = new ArrayList<String>();
							newListOfFolderNames.add(folderName);
							newListOfFolderNames.trimToSize();
							folderNameMapping.put(exponentString, newListOfFolderNames);
						}
					}
				}
				fileCounter++;
			}
//			System.out.println("fileList.length=" + fileList.length + "  fileCounter=" + fileCounter);		
			currentFolder = null;
		}
		inputFolder = null;
//		System.exit(0);
		

		Iterator<String> keyIterator = existingRuns.keySet().iterator();
		
		while(keyIterator.hasNext()){
			String currentKey = keyIterator.next();
//			System.out.println(currentKey);
			
			ArrayList<Integer> currentIndices = existingRuns.get(currentKey);
			
			currentIndices.trimToSize();
			
			Collections.sort(currentIndices);
			
//			System.out.println(currentKey + ": " + currentIndices.size());
			
			
//			System.out.println(currentKey + " (" + currentIndices.size() + " entries):");
//			for(Integer indexEntry: currentIndices){
//				System.out.print(indexEntry + "\t");
//			}
//			System.out.println("\n");
			
			if(currentIndices.size() != 401){
				System.out.println("Exponent " + currentKey + " has " + currentIndices.size() + " entries instead of 401");
			}
		}
		
		
		
		
		keyIterator = folderNameMapping.keySet().iterator();
		
		int numberOfFoldersRenamed = 0;
		
		while(keyIterator.hasNext()){
			String currentKey = keyIterator.next();
//			System.out.println(currentKey);
			
			ArrayList<String> currentFolderNames = folderNameMapping.get(currentKey);
			
			currentFolderNames.trimToSize();
			
//			System.out.println(currentKey + ": " + currentFolderNames.size());
			
			
//			System.out.println(currentKey + " (" + currentFolderNames.size() + " entries):");
//			for(String folderNameEntry: currentFolderNames){
//				System.out.print(folderNameEntry + "\t");
//			}
//			System.out.println("\n");
			
			
			
			for(String folderNameEntry: currentFolderNames){
				String oldFolderName = inputPath + folderNameEntry;
				File folderToRename = new File(oldFolderName);
//				System.out.println(folderToRename.getAbsolutePath());
				
				String newFolderName = inputPath + currentKey + "_" + folderNameEntry;
//				System.out.println("old name=\"" + oldFolderName + "\"\tnew name=\"" + newFolderName + "\"");
				
				File newFolder = new File(newFolderName);
				
				System.out.println("renaming folder " + oldFolderName + " to " + newFolderName);
				if(folderToRename.renameTo(newFolder)){
					numberOfFoldersRenamed++;
				}
				folderToRename = null;
			}			
		}
		System.out.println("Number of folders renamed = " + numberOfFoldersRenamed);		
	}/*  end of main method  */
}/*  end of LOOCVextractor class  */
